import {
    permissionTreeList,
    permissionAdd,
    permissionUpdate,
    permissionDelete
} from "@/api/permission";
import { fitlerTreeList } from "@/utils/filterPermissionTreeList";

export const permissionStore = {
    namespaced: true,
    state:{
        treeList:[],
        filteredTreeList:[],
        unAssignTreeList:[],
        rowPermId:0,
        rowPermLevel:0,
        rowPermForUpdate:{
            pid:0,
            type:0,
            level:0,
            id:0
        }
    },
    mutations:{
       setTreeList(state,data){
            state.treeList = data;
       },
       setFilteredTreeList(state, data){
            state.filteredTreeList = data;
       },
       setUnAssignTreeList(state, data){
            state.unAssignTreeList = data;
       },
       setRowPermId(state,data){
            state.rowPermId = data;
       },
       setRowPermLevel(state, data){
            state.rowPermLevel = data
       },
       setRowPermForUpdate(state,data){
            state.rowPermForUpdate = data;
       }
    },
    actions:{
        //  获取所有权限(树型结构)
        async permissionTreeList(context){
            try{
                const result = await permissionTreeList();
                if(result.code === 200){
                    context.commit("setTreeList",result.data);
                    return result;
                } else {
                    return Promise.reject(new Error(result.msg) || "获取数据失败");
                }
            } catch(error){
                 return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  处理权限过滤和选中状态(赋予权限)
        async getFilteredTreeList(context,data){
            const [allPerms, rolePermIds] = data;
            const list = fitlerTreeList(allPerms, rolePermIds);
            context.commit("setFilteredTreeList",list);
        },
        //  处理权限过滤和选中状态(移除权限)
        async getUnassignPermTreeList(context,data){
            const [rolePerm,treeList] = data;
            const idArr = rolePerm.id;
            const filterTree = (nodes) => {
                return nodes
                    .filter(node => {
                        if (idArr.includes(node.id)) {
                            return true; 
                        }
                        if (node.children && node.children.length > 0) {
                            const filteredChildren = filterTree(node.children);
                            if (filteredChildren.length > 0) {
                                node.children = filteredChildren; 
                                return true; 
                            }
                        }
                        return false;
                    });
            };
            const unAssignTreeList = filterTree(treeList);
            context.commit("setUnAssignTreeList",unAssignTreeList);
        },
        //  新增权限
        async permissionAdd(context, data){
            try{
                const result = await permissionAdd(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "添加数据失败");
                }
            } catch (error) {
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  修改权限
        async permissionUpdate(context, data){
            try{
                const result = await permissionUpdate(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "添加数据失败");
                }
            } catch (error) {
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  删除权限
        async permissionDelete(contetx, data){
            const { id } = data;
            try{
                const result = await permissionDelete(id);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "添加数据失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }   
        }
    },
    getters:{
        filteredTreeList: (state) => state.filteredTreeList,
        treeList: (state) => state.treeList,
        unAssignTreeList: (state) => state.unAssignTreeList
    }
}